package website.seamonster.ethegg.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import website.seamonster.ethegg.dataobject.Egg;
import website.seamonster.ethegg.enums.EggStatusEnum;
import website.seamonster.ethegg.repository.EggRepository;

import java.math.BigDecimal;

/**
 * Created by 罗海量
 * 2018-07-23 23:28
 */
@lombok.extern.slf4j.Slf4j
@Service
public class EggService {
    @Autowired
    private EggRepository eggRepository;

    /**
     * 判断购买的数量是否超过现有出售中的蛋的数量
     * @param buyCnt    购买数量
     * @return
     */
    public Boolean isEggEnough(Integer buyCnt){
        Long eggNum = this.getSellingEggNum();
        return eggNum >= buyCnt;
    }

    /**
     * 剩余蛋数
     * @return
     */
    public Long getSellingEggNum(){
        return eggRepository.getSellingEggNum();
    }

    /**
     * 当前蛋价
     * @param userId
     * @return
     */
    public BigDecimal getEggPrice(String userId){
        Egg egg = eggRepository.findEggToBuy(userId);
        return egg.getPrice();
    }

    /**
     * 属于该用户的孵化中的蛋
     * @param userId
     * @return
     */
    public Integer getBroodEggsCountByUserId(String userId){
        return eggRepository.getBroodEggsCountByUserId(userId, EggStatusEnum.BROOD.getCode());
    }
}
